<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="eatpoopyoucat.EpycGame" %>
<%@ page import="eatpoopyoucat.PMF" %>

<html>
	<head>
		<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
	</head>

	<body>
<%
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
if (user != null) {
 %>
 		<p>Hello, <%= user.getNickname() %>! (You can
		<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out</a>.)</p>
 <%
	PersistenceManager pm = PMF.get().getPersistenceManager();
	String query = "select from " + EpycGame.class.getName();
	List<EpycGame> games = (List<EpycGame>) pm.newQuery(query).execute();
	if (games.isEmpty()) {
%>
		<p>There are no games currently.</p>
<%
	} else {
		for (EpycGame g : games) {
%>
		<p><b><a href="game.jsp?gid=<%= g.getKey().getId() %>"><%= g.getKey().getId() %> started on <%= g.getStartDate().toString() %></a></b></p>
<%
		}
	}
%>
		<p>Want to start a new game?</p>
		<form action="/new" method="post">
			<div><p>First message</p><textarea name="content" rows="3" cols="60"></textarea></div>
			<div><p>Email addresses of players. Enter your address first, followed by at least one other player. Separate addresses with a semi-colon (;). <b>NO SPACES!</b></p><textarea name="userList" rows="3" cols="60"></textarea></div>
			<div><input type="submit" value="Start Game" /></div>
		</form>
<%
	pm.close();
} else {
%>
		<p>Please log in to use this form. <a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign in</a></p>
<%
}
%>
	</body>
</html>